﻿using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace PageOffice6_Net_Core.Controllers.SendParameters
{
    public class SendParametersController : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        public SendParametersController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }
        public IActionResult Word()
        {
            PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);
            //设置保存页面
            poCtrl.SaveFilePage = "SaveDoc?id=1";//传递查询参数
            //打开Word文档
            poCtrl.WebOpen("doc/test.doc", PageOfficeNetCore.OpenModeType.docNormalEdit, "tom");
            ViewBag.poCtrl = poCtrl.GetHtml();
            return View();
        }


        public async Task<ActionResult> SaveDoc(int id, string userName, int age, string selSex)
        {
            PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
            await fs.LoadAsync();
            //await fs.LoadAsync();
            string webRootPath = _webHostEnvironment.WebRootPath;
            fs.SaveToFile(webRootPath + "/SendParameters/doc/" + fs.FileName);

            if (userName != null && userName.Trim().Length > 0)
            {
                userName = fs.GetFormField("userName");
                userName = HttpUtility.UrlDecode(userName);
            }

            if (selSex != null && selSex.Trim().Length > 0)
            {
                selSex = fs.GetFormField("selSex");
                selSex = HttpUtility.UrlDecode(selSex);

            }

            fs.ShowPage(300, 200,this); // 显示一下SaveFile.aspx获取到的所有参数的值

            string content = "";
            content += "传递的参数为：<br />";
            content += " userName:" + userName + "<br />";
            content += " id:" + id + "<br />";
            content += " age:" + age + "<br />";
            content += " sex:" + selSex + "<br />";

            //await Response.Body.WriteAsync(Encoding.GetEncoding("gbk").GetBytes(content));
            fs.Close();
            ViewBag.content = content;
            return View();
        }
    }
}